package com.np.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.np.shop.entity.TbSpecification;
import com.np.shop.entity.TbSpecificationOption;
import com.np.shop.mapper.TbSpecificationMapper;
import com.np.shop.mapper.TbSpecificationOptionMapper;
import com.np.shop.service.ITbSpecificationService;
import com.np.shop.utils.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李高帅
 * @since 2020-11-27
 */
@Service
public class TbSpecificationServiceImpl extends ServiceImpl<TbSpecificationMapper, TbSpecification> implements ITbSpecificationService {
    @Resource
    private TbSpecificationMapper tbSpecificationMapper;
    @Resource
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;
    @Override
    public PageResult<TbSpecification> selectList(Map map, Integer page, Integer size) {
        QueryWrapper<TbSpecification> queryWrapper=new QueryWrapper<>();
        if (map!=null){
            if (map.get("specName") !=null && !"".equals("specName")){
                queryWrapper.lambda().like(TbSpecification::getSpecName,"%"+map.get("specName")+"%");
            }
        }
        Page<TbSpecification> page1=tbSpecificationMapper.selectPage(new Page<>(page,size),queryWrapper);
        return new PageResult<>(page1.getTotal(),page1.getRecords());
    }

    /**
     * 添加规格
     * @param tbSpecification
     * @return
     */
    @Transactional
    @Override
    public void addSpecification(TbSpecification tbSpecification) {
        //先添加规格 主表
       tbSpecificationMapper.insert(tbSpecification);

        //在添加  规格选项
        for (TbSpecificationOption tbSpecificationOption:tbSpecification.getTbSpecificationOptions()){
            //两张表简历关系
            tbSpecificationOption.setSpecId(tbSpecification.getId());
            //添加规格选项表
            tbSpecificationOptionMapper.insert(tbSpecificationOption);
        }

    }
}
